package main;

import image.ImgPainter;
import image.Matrix;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.image.BufferStrategy;
import java.io.IOException;

public class View extends Frame implements Runnable, WindowListener {
	private static final long serialVersionUID = 1L;

	private static final long FRAME_DELAY = 16;
	private Thread thread;
	private boolean go;
	private long cycleTime, refreshTime;
	private ImgPainter img;
	private int updateCounter;
	Dimension oldDim;

	private Point redPoint;

	private String info;

	private Point greenPoint;

	private Matrix matrix;

	public View() {
		super("title");
		img = null;
		oldDim = new Dimension();
		setSize(300, 550);
		setLocation(510, 30);
		setLayout(null);
		setIgnoreRepaint(true);
		setBackground(null);
		thread = null;
		updateCounter = 1;
		addWindowListener(this);

	}

	@Override
	public void run() {
		cycleTime = System.currentTimeMillis();
		createBufferStrategy(3);
		BufferStrategy strategy = getBufferStrategy();
		while (go) {
			try {
				updateGameState();
				updateGUI(strategy);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private void updateGameState() {
		
	}

	private void updateGUI(BufferStrategy strategy) {
		if (paintNeeded()) {
			Graphics g = strategy.getDrawGraphics();
			
			if (null != img) {

				
				img.paint(g, null, 0, 0, getWidth(), getHeight());
				redPoint = ImgPainter.findColoredPoint(img.img, Color.red, 105);
				greenPoint = ImgPainter.findColoredPoint(img.img, Color.green, 105);
				redPoint = img.Dupa(getSize(), redPoint);
				greenPoint = img.Dupa(getSize(), greenPoint);
				if (null != redPoint){
					g.setColor(Color.red);
					g.fillRect(redPoint.x, redPoint.y,  20,
							 20);
				}
				if (null != greenPoint){
					g.setColor(Color.green);
					g.fillRect(greenPoint.x, greenPoint.y,  20,
							 20);
				}
				ImgPainter.makeGrid(g,getSize(), 50,50, matrix);
				//g.drawString(info, 10, 10);
			} else {
				g.setColor(Color.pink);
				g.fillRect(0, 0, getWidth() / 2, getHeight());
				g.setColor(Color.blue);
				g.fillRect(getWidth() / 2, 0, getWidth(), getHeight());
				g.setColor(Color.BLACK);
			}
			// logic
			// mvc.model.paint(g);

			g.dispose();
			synchFramerate();
			strategy.show();
		} else
			synchFramerate();
	}

	private boolean paintNeeded() {
		Dimension dim = getSize();
		long currentTime = System.currentTimeMillis();
		if (dim.height != oldDim.height || dim.width != oldDim.width
				|| updateCounter > 0 || refreshTime + 50 < currentTime) {
			oldDim = dim;
			updateCounter = 0;
			refreshTime = currentTime;
			return true;
		}
		return false;
	}

	private void synchFramerate() {
		// System.out.println("synch " + cycleTime);
		cycleTime = cycleTime + FRAME_DELAY;
		long difference = cycleTime - System.currentTimeMillis();
		try {
			Thread.sleep(Math.max(0, difference));
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public void start() {
		go = true;
		if (thread == null) {
			thread = new Thread(this);
			thread.start();
		}
	}

	public void stop() {
		go = false;
		Thread.yield();
		join();
		thread = null;
	}

	public void join() {
		try {
			thread.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public void init() {
	}

	public void setup() {
		try {
			img = new ImgPainter(
					"C:\\!_Eclipse\\Marcela\\trunk\\Images\\wersja1.1.jpg");
			matrix = img.createMatrix(50, 50);
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public void update(Graphics g) {

	}

	public void paint(Graphics g) {

	}

	public void repaint() {

	}

	public void end() {
		stop();
		setVisible(false);
		// bufferGraphics.dispose();
		dispose();
	}

	public void upd() {
		updateCounter++;
	}

	@Override
	public void windowActivated(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowClosed(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowClosing(WindowEvent arg0) {
		end();
	}

	@Override
	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}
}
// //////////////////////////
